49_ADC Configuration
The ADC needed to be configured properly for long term use. We probably will need it to be in “continuous conversion” mode, so that it continues converting values after the START/SYNC (0x0008) command is sent. To do this, the WREG command is used to set the CM bit in the ADC’s Configuration Register 1 (Figure 1).
Figure 1: ADC Register 0x01
There was a problem writing configuration data to the ADC’s register. Since the ADC receive’s commands in 8-bit form, writing 16-bit values to the ADC was tricky. For commands such as START/SYNC, this was not a problem, but the WREG command for example requires a first byte indicating which register to write to and how many bytes to write. It also requires a second byte that contains the information to be written, but each command needs to be separated by a certain amount of time, so I could not include both bytes in a single 16-bit transmission.
I tried modifying the SPI2 module’s communication form from 16-bit to 8-bit before ADC commands were given and back to 16-bit communication after commands were done, but the SPI module did not seem to allow any mode changes after it had been originally set. I made sure to disable the SPI module before trying to modify it’s mode configurations, but that didn’t work as well. So I had to transmit commands in 16-bit (2 byte) form as in Figure 2. I had actually tried this previously with the SPI1 module, but I noticed that I was doing the WREG command wrong. In order to write two bytes to the Config Register 1, a WREG command of 0x44 needs to be sent. It should be followed by a command contain the information to be written. I found out that I had previously been writing the 16-bit commands wrong, resulting in the WREG command actually clearing all the bits in the Config Register 1 instead of setting the CM bit. Since I was assuming continuous conversion was enabled when I was trying the debug the SPI1 module, this may have been the reason why I was having problems receiving data. But after working out how to position the 8-bit commands in the 16-bit transmissions, the ADC_init() function seemed to work, as the SPI2 module was receiving continuous information after only one SYNC/START command.
I may try to go back and get the SPI1 module to work.
Figure 2: ADC Configuration Code